package com.example.wallet.db

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.wallet.bean.AccountInfo
import com.example.wallet.bean.UserInfoWrapper
import com.example.wallet.global.AppGlobals

@Database(entities = [AccountInfo::class, UserInfoWrapper::class], version = 5)
abstract class AccountDatabase : RoomDatabase() {

    companion object {
        @Volatile
        private var DATA_BASE: AccountDatabase? = null

        fun getInstance() = DATA_BASE ?: synchronized(this) {
            DATA_BASE ?: buildDataBase().also { DATA_BASE = it }
        }

        private fun buildDataBase() = Room.databaseBuilder(
            AppGlobals.getApplication(),
            AccountDatabase::class.java,
            "Account_Info"
        ).allowMainThreadQueries()
            .fallbackToDestructiveMigration()
            .build()
    }

    abstract fun getAccountInfoDao(): AccountInfoDao

    abstract fun getUserInfoDao(): UserInfoDao

}